<?php
	namespace Common\Logic;
	use Common\Model\UserModel;
	class UserLogic extends UserModel{
		/*
		* Function:		getUserList
		* Description:	用户列表
		* @param:		$page string 起始页
						$filter array 过滤数组
						$pageSize string 每页显示条数
		*/
		public function getUserList($page=1,$filter = '',$pageSize=20){
			$conditon = $this->getUserFilter($filter);
			return $this->where($conditon)->order('id desc')->limit(($page-1)*$pageSize,$pageSize)->select();
		}
		
		/*
		* Function:get:	getUserFilter
		* Description:	用户列表搜索过滤项
		* @param:		$filter array 过滤数组
		*/
		public function getUserFilter($filter = ''){
			$conditon = array();
			if(!empty($filter['id'])){
				$conditon['id'] = $filter['id'];
			}
			if(!empty($filter['nickname'])){
				$conditon['nickname'] = $filter['nickname'];
			}
			if(!empty($filter['name'])){
				$conditon['name'] = $filter['name'];
			}
			if(!empty($filter['mobile'])){
				$conditon['mobile'] = $filter['mobile'];
			}
			if(!empty($filter['username'])){
				$conditon['username'] = $filter['username'];
			}
			return $conditon;
		}
		
		/*
		* Function:get:	getUserCount
		* Description:	用户列表总数
		* @param:		$filter array 过滤数组
		*/
		public function getUserCount($filter = ''){
			$conditon = $this->getUserFilter($filter);
			return $this->where($conditon)->count();
		}
		
		/*
		* Function:		getUsersBySessionId
		* Description:  获取会员信息——通过SessionId
		* Params:		$real_name string 姓名，$id int 用户id
		*/
		public function getUsersBySessionId($sessionId,$id = ''){
			$conditon = array();
			if(!empty($sessionId)){
				$conditon['sessionid'] = $sessionId;
			}
			if(!empty($id)){
				$conditon['id'] = array('neq',$id);
			}
			return $this->where($conditon)->find();
		}
		
		/*
		* Function:		addUser
		* Description:	增加用户
		* @param:		$data array 增加的数组
		*/
		public function addUser($data){
			return $this->add($data);
		}
		
		/*
		* Function:		getUserInfo
		* Description:	获取用户信息 by row
		* @param:		$id int ID
		*/
		public function getUserInfo($id){
			return $this->where(array('id'=>$id))->find();
		}
		
		/*
		* Function:		updateUser
		* Description:	更新用户
		* @param:		$data array 更新的数组
		*/
		public function updateUser($data){
			return $this->save($data);
		}
		
		/*
		* Function:		deleteUser
		* Description:	删除用户
		* @param:		$id int ID
		*/
		public function deleteUser($id){
			return $this->where(array('id'=>$id))->delete();
		}

		/*
		* Function:		getUsersAll
		* Description:	获取会员列表信息 by row
		* @param:		$id int ID
		*/
		public function getUsersAll($id){
			return $this->select();
		}
		
		/*
		* Function:		getUsersInfoByNumberNo
		* Description:	获取会员列表信息 by number_no
		* @param:		$id int ID
		*/
		public function getUsersInfoByNumberNo($numberNo = ''){
			return $this->where(array('number_no'=>$numberNo))->find();
		}
		
		/*
		* Function:		getUsersInfoByOpenId
		* Description:	获取会员列表信息 by openid
		* @param:		$id int ID
		*/
		public function getUsersInfoByOpenId($openid){
			return $this->where(array('openid'=>$openid))->find();
		}
		
		/*
		* Function:		getUsersInfoBySessionId
		* Description:	获取会员列表信息 by sessionid
		* @param:		$id int ID
		*/
		public function getUsersInfoBySessionId($sessionid){
			return $this->where(array('sessionid'=>$sessionid))->find();
		}

		/*
		* Function:		generateNumberNo
		* Description:	生成会员编号——唯一
		*/
		public function generateNumberNo(){
			$numberNo = mt_rand(10000000,99999999);
			if($this->getUsersInfoByNumberNo($numberNo)){
				return $this->generateNumberNo();
			}
			return $numberNo;
		}
		
		/*
		* Function:		getUsersInfoByToken
		* Description:	获取会员列表信息 by sessionid
		* @param:		$id int ID
		*/
		public function getUserInfoByToken($sessionid){
            if(!$info = $this->where(array('sessionid'=>$sessionid))->find()){
                return ['code'=>-1,'errorMsg'=>'token不存在，请重新登录'];
            }
            return ['code'=>1,'user_id'=>$info['id'], 'username' => $info['username'], 'name' => $info['name'], 'contract' => $info['contract'], 'role' => $info['role'], 'mobile' => $info['mobile']];
		}
	}
?>